package org.jeecg.modules.paycenter.channel.zzbank.util;

import java.io.*;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jeecg.modules.wlhy.utils.R;
import org.springframework.beans.factory.annotation.Value;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import sun.net.www.protocol.http.HttpURLConnection;
@Slf4j
public class ZhengzhouBankUtil {
    public Document zzNoerrorsbankPayment(String pathUrl, String trsCode, String erpJnlNo, String xDataBody) {
        String stringXml = null;
        NodeList nodeList = null;
        Document doc = null;
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        sb.append("<Message>");
        sb.append("<Head>");
        sb.append("<TrsCode>" + trsCode + "</TrsCode>");
        sb.append("<ERPTrsTimestamp>" + dateFormat.format(date) + "</ERPTrsTimestamp>");
        sb.append("<ERPJnlNo>" + erpJnlNo + "</ERPJnlNo>");
        sb.append("</Head>");
        sb.append("<Body>");
        sb.append(xDataBody);
        sb.append("</Body>");
        sb.append("</Message>");
        log.info("请求报文为：{},{}", pathUrl, sb.toString());
        HttpClient httpClient = HttpClientBuilder.create().build();
        StringEntity stringEntity = new StringEntity(sb.toString(), "UTF-8");
        stringEntity.setContentEncoding("UTF-8");
        stringEntity.setContentType("application/x-NS-BDES");
        System.out.println(pathUrl);
        HttpPost post = new HttpPost(pathUrl);
        post.setEntity(stringEntity);
        HttpResponse response = null;
        String result = null;
        String returnMsg = "请求出错";

        try {
            response = httpClient.execute(post);
            HttpEntity httpEntity = response.getEntity();
            result = EntityUtils.toString(httpEntity, "UTF-8");
            log.info("郑州银行返回信息"+result);
            if (result != null && !result.equals("")) {
                stringXml = result.trim();
                System.out.println("郑州银行返回信息" + stringXml);
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                doc = builder.parse(new InputSource(new StringReader(stringXml)));
                String code = doc.getElementsByTagName("IBSReturnCode").item(0).getFirstChild().getNodeValue();
                if (code == null || !code.equals("000000")) {
                    returnMsg = doc.getElementsByTagName("IBSReturnMsg").item(0).getFirstChild().getNodeValue();
                }
            }
        } catch (Exception var21) {
        }

        return doc;
    }

    public Document zzbankPayment(String pathUrl, String trsCode, String erpJnlNo, String xDataBody) throws Exception {
        String stringXml = null;
        NodeList nodeList = null;
        Document doc = null;
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        sb.append("<Message>");
        sb.append("<Head>");
        sb.append("<TrsCode>" + trsCode + "</TrsCode>");
        sb.append("<ERPTrsTimestamp>" + dateFormat.format(date) + "</ERPTrsTimestamp>");
        sb.append("<ERPJnlNo>" + erpJnlNo + "</ERPJnlNo>");
        sb.append("</Head>");
        sb.append("<Body>");
        sb.append(xDataBody);
        sb.append("</Body>");
        sb.append("</Message>");
        log.info("虚户转账请求报文为：{},{}", pathUrl, sb);
        HttpClient httpClient = HttpClientBuilder.create().build();
        StringEntity stringEntity = new StringEntity(sb.toString(), "UTF-8");
        stringEntity.setContentEncoding("UTF-8");
        stringEntity.setContentType("application/x-NS-BDES");
        System.out.println(pathUrl);
        HttpPost post = new HttpPost(pathUrl);
        post.setEntity(stringEntity);
        HttpResponse response = null;
        String result = null;
        String returnMsg = "银行请求出错，请稍后重试";

        try {
            response = httpClient.execute(post);
            HttpEntity httpEntity = response.getEntity();
            result = EntityUtils.toString(httpEntity, "UTF-8");
        } catch (IOException var21) {
            throw new Exception(returnMsg);
        }

        try {
            stringXml = result.trim();
            log.info("虚户转账郑州银行返回信息"+result);
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            doc = builder.parse(new InputSource(new StringReader(stringXml)));
            String code = doc.getElementsByTagName("IBSReturnCode").item(0).getFirstChild().getNodeValue();
            if (code != null && code.equals("000000")) {
                return doc;
            } else {
                returnMsg = doc.getElementsByTagName("IBSReturnMsg").item(0).getFirstChild().getNodeValue();
                throw new Exception(returnMsg);
            }
        } catch (Exception var20) {
            throw new Exception(returnMsg);
        }
    }

    public R zzFilebankPayment(String pathUrl) throws Exception {
        String stringXml = null;
        log.info("下载回单请求地址：" + pathUrl);
        URL url = new URL(pathUrl);
        log.info("下载回单请求地址转换后：" + url.getPath());
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");
        // 获取响应码
        int responseCode = conn.getResponseCode();
        log.info("Response Code: " + responseCode);
        if (responseCode == HttpURLConnection.HTTP_OK) { // 200
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            stringXml = response.toString().trim();
            if (stringXml.contains("success")) {
                return R.ok(stringXml);
            } else {
                return R.failed(stringXml);
            }
        }

        return R.failed();
    }
}
